'''基础层：实现元素的操作'''
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep
import logging

logging.basicConfig(level=logging.INFO, filename='baidu.log',format='%(asctime)s %(levelname)s %(name)s %(message)s')
class BasePage:
    def __init__(self, dr):
        self.dr = dr
    def get_url(self, url):
        ''' 实现打开url页面 '''
        self.dr.get(url)
        self.dr.maximize_window()
    def find_ele(self,loc):
        '''实现元素定位'''
        ele = self.dr.find_element(*loc)
        return ele
        logging.info(f'定位{loc}成功')
    def input_element(self,loc,value):
        '''实现输入框数据输入'''
        ele = self.find_ele(loc)
        ele.send_keys(value)
        logging.info(f'在{loc}输入{value}成功')
    def element_click(self,loc):
        '''实现搜索按钮的点击'''
        ele = self.find_ele(loc)
        ele.click()
        logging.info(f'点击{loc}成功')
    def element_clear(self,loc):
        '''清空操作'''
        ele = self.find_ele(loc)
        ele.clear()
        logging.info(f'在{loc}清除文本成功')
if __name__ == '__main__':
    url = 'https://www.baidu.com/'
    kw_loc = (By.ID, 'kw')
    su_loc = (By.ID, 'su')
    value = 'selenium'
    chrome_location = r'D:\chrom browser\chrome-win\chrome.exe'
    options = webdriver.ChromeOptions()
    options.binary_location = chrome_location
    dr = webdriver.Chrome(options=options)
    # dr.maximize_window()
    base_page = BasePage(dr)
    base_page.get_url(url)
    base_page.element_clear(kw_loc)
    base_page.input_element(kw_loc, value)
    base_page.element_click(su_loc)
    sleep(3)
    dr.quit()
